package com.xian.ucenter.controller.admin;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xian.ucenter.commons.Result;
import com.xian.ucenter.entities.Post;
import com.xian.ucenter.entities.Tag;
import com.xian.ucenter.entities.vo.PostQuery;
import com.xian.ucenter.entities.vo.PostVo;
import com.xian.ucenter.entities.vo.TagNames;
import com.xian.ucenter.service.PostService;
import com.xian.ucenter.service.PostTagService;
import com.xian.ucenter.service.TagService;
import com.xian.ucenter.service.impl.PostServiceImpl;
import com.xian.ucenter.utils.PageUtil;
import javafx.geometry.Pos;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 文章管理表 前端控制器
 * </p>
 *
 * @author xian
 * @since 2022/07/23 02:59
 */
@RestController
@RequestMapping("/ucenter/admin/post")
public class PostController {
    @Autowired
    private PostServiceImpl postService;
    @Autowired
    private TagService tagService;
    @Autowired
    private PostTagService postTagService;
    @Resource
    private PageUtil<Post> postPageUtil;

    @PostMapping("/save")
    public Result save (@RequestBody PostVo postVo) {
        return postService.savePostVo(postVo);
    }

    @PostMapping("/create")
    public Result create (@RequestBody Post post) {
        long count = postService.count(new QueryWrapper<Post>().like("title", "新建帖子"));
        if (count > 0) {
            post.setTitle("新建帖子("+count+")");
        } else {
            post.setTitle("新建帖子");
        }
        post.setContent("");
        return postService.save(post) ? Result.ok("postId",post.getId()):Result.fail();
    }

    @GetMapping("/{postId}")
    public Result getPost (@PathVariable("postId") String postId) {
        Post post = postService.getById(postId);
        if (ObjectUtil.isNull(post))
            throw new RuntimeException("帖子不存在");
        PostVo postVo = BeanUtil.copyProperties(post, PostVo.class);
        List<String> tags = tagService.getTagIdsByPostId(post.getId());
        postVo.setList(tags);
        return Result.ok("post",postVo);
    }

    @PostMapping("/search/{current}/{count}")
    public Result getPost (@PathVariable("current") Integer current, @PathVariable("count") Integer count, @RequestBody(required = false)PostQuery postQuery) {
        QueryWrapper<Post> postQueryWrapper = new QueryWrapper<>();
        if (ObjectUtil.isNotNull(postQuery)) {
            String title = postQuery.getTitle();
            Integer status = postQuery.getStatus();
            Date begin = postQuery.getCreateTime();
            Date end = postQuery.getModifyTime();
            if (StringUtils.hasText(title)) {
                postQueryWrapper.like("title",title);
            } else if (ObjectUtil.isNotNull(status)) {
                postQueryWrapper.eq("status",status);
            } else if (ObjectUtil.isNotNull(begin)) {
                postQueryWrapper.lt("create_time",begin);
            } else if (ObjectUtil.isNotNull(end)) {
                postQueryWrapper.gt("create_time",end);
            }
        }
        return postPageUtil.pageList(current,count,postService,postQueryWrapper);
    }

    @GetMapping("/del/{postId}")
    public Result delPost (@PathVariable("postId") String postId) {
        postTagService.removePostTagByPostId(postId);
        return postService.removeById(postId)?Result.ok():Result.fail();
    }

}
